<template>
  <div>
    <el-card class="!border-none mb-4" shadow="never">
      <el-form
          class="mb-[-16px]"
          :model="queryParams"
          inline
      >
        <el-form-item label="退款编号" prop="sn">
          <el-input class="w-[280px]" v-model="queryParams.sn" clearable placeholder="请输入退款编号"/>
        </el-form-item>
        <el-form-item label="关联用户" prop="user_id">
          <el-input class="w-[280px]" v-model="queryParams.user_id" clearable placeholder="请输入关联用户"/>
        </el-form-item>
        <el-form-item label="来源订单id" prop="order_id">
          <el-input class="w-[280px]" v-model="queryParams.order_id" clearable placeholder="请输入来源订单id"/>
        </el-form-item>
        <el-form-item label="来源单号" prop="order_sn">
          <el-input class="w-[280px]" v-model="queryParams.order_sn" clearable placeholder="请输入来源单号"/>
        </el-form-item>
        <el-form-item label="第三方平台交易流水号" prop="transaction_id">
          <el-input class="w-[280px]" v-model="queryParams.transaction_id" clearable
                    placeholder="请输入第三方平台交易流水号"/>
        </el-form-item>
        <el-form-item label="退款状态，0退款中，1退款成功，2退款失败" prop="refund_status">
          <el-input class="w-[280px]" v-model="queryParams.refund_status" clearable
                    placeholder="请输入退款状态，0退款中，1退款成功，2退款失败"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="!border-none" v-loading="pager.loading" shadow="never">
      <el-button v-perms="['order_refund_record/add']" type="primary" @click="handleAdd">
        <template #icon>
          <icon name="el-icon-Plus"/>
        </template>
        新增
      </el-button>
      <el-button
          v-perms="['order_refund_record/delete']"
          :disabled="!selectData.length"
          @click="handleDelete(selectData)"
      >
        删除
      </el-button>
      <div class="mt-4">
        <el-table :data="pager.lists" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55"/>
          <el-table-column label="退款编号" prop="sn" show-overflow-tooltip/>
          <el-table-column label="关联用户" prop="user_id" show-overflow-tooltip/>
          <el-table-column label="来源订单id" prop="order_id" show-overflow-tooltip/>
          <el-table-column label="来源单号" prop="order_sn" show-overflow-tooltip/>
          <!--          <el-table-column label="订单来源 order-商品订单 recharge-充值订单" prop="order_type" show-overflow-tooltip/>-->
          <!--          <el-table-column label="订单总的应付款金额，冗余字段" prop="order_amount" show-overflow-tooltip/>-->
          <el-table-column label="本次退款金额" prop="refund_amount" show-overflow-tooltip/>
          <el-table-column label="第三方平台交易流水号" prop="transaction_id" show-overflow-tooltip/>
          <!--          <el-table-column label="退款方式 1-线上退款 2-线下退款" prop="refund_way" show-overflow-tooltip/>-->
          <!--          <el-table-column label="退款类型 1-后台退款" prop="refund_type" show-overflow-tooltip/>-->
          <el-table-column label="状态" prop="refund_status">
            <!--            退款状态，0退款中，1退款成功，2退款失败-->
            <template #default="{row}">
              <el-tag :type="tagEnum[row.refund_status]">{{ tagText[row.refund_status] }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200" fixed="right">
            <template #default="{ row }">
              <el-button size="small" :disabled="row.refund_status > 0" type="primary" @click="confirmHandle(row)">审核</el-button>
              <el-button size="small" :disabled="row.refund_status > 0" type="danger" @click="closeHandle(row)">驳回</el-button>
              <!--              <el-button-->
              <!--                  v-perms="['order_refund_record/edit']"-->
              <!--                  type="primary"-->
              <!--                  link-->
              <!--                  @click="handleEdit(row)"-->
              <!--              >-->
              <!--                编辑-->
              <!--              </el-button>-->
              <!--              <el-button-->
              <!--                  v-perms="['order_refund_record/delete']"-->
              <!--                  type="danger"-->
              <!--                  link-->
              <!--                  @click="handleDelete(row.id)"-->
              <!--              >-->
              <!--                删除-->
              <!--              </el-button>-->
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="flex mt-4 justify-end">
        <pagination v-model="pager" @change="getLists"/>
      </div>
    </el-card>
    <edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false"/>
  </div>
</template>

<script lang="ts" setup name="orderRefundRecordLists">
import {usePaging} from '@/hooks/usePaging'
import {useDictData} from '@/hooks/useDictOptions'
import {
  apiOrderRefundRecordLists,
  apiOrderRefundRecordDelete,
  apiConfirmOrder,
  apiCancelOrder
} from '@/api/order_refund_record'
import {timeFormat} from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import {ElMessage, ElMessageBox} from 'element-plus'
import {computed} from "vue";

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)


const tagEnum = computed(() => {
  // 0退款中，1退款成功，2退款失败
  return {
    '0': '',
    '1': 'success',
    '2': 'danger'
  }
})
const tagText = computed(() => {
  return {
    '0': '退款中',
    '1': '退款成功',
    '2': '退款失败'
  }
})

// 查询条件
const queryParams = reactive({
  sn: '',
  user_id: '',
  order_id: '',
  order_sn: '',
  transaction_id: '',
  refund_status: ''
})

function closeHandle({id}) {
  ElMessageBox.confirm(
      '确定驳回申请吗?',
      '提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(async () => {
        await apiCancelOrder({id})
        ElMessage.success({message: '操作成功'})
        await getLists()
      })
      .catch(() => {

      })
}

function confirmHandle(row: any) {
  const {id} = row
  ElMessageBox.confirm(
      '确定审核通过吗?',
      '提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(async () => {
        await apiConfirmOrder({id})
        ElMessage.success({message: '审核成功'})
        await getLists()
      })
      .catch(() => {

      })
}

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
  selectData.value = val.map(({id}) => id)
}

// 获取字典数据
const {dictData} = useDictData('')

// 分页相关
const {pager, getLists, resetParams, resetPage} = usePaging({
  fetchFun: apiOrderRefundRecordLists,
  params: queryParams
})

// 添加
const handleAdd = async () => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('edit')
  editRef.value?.setFormData(data)
}

// 删除
const handleDelete = async (id: number | any[]) => {
  await feedback.confirm('确定要删除？')
  await apiOrderRefundRecordDelete({id})
  getLists()
}

getLists()
</script>

